#include <iostream>
#include <vector>
#include <cmath>
#include "matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

double f(double x)
{
    double ret = x*x*x*x*x*x*x*x;
    ret -= 8*x*x*x*x*x*x*x;
    ret += 28 *x*x*x*x*x*x;
    ret -= 56*x*x*x*x*x;
    ret += 70*x*x*x*x;
    ret -= 56*x*x*x;
    ret += 28*x*x;
    ret -= 8*x;
    ret += 1;
    return ret;
}

double g(double x)
{
    double ret = x - 8;
    ret = ret * x + 28;
    ret = ret * x - 56;
    ret=ret*x+70;
    ret=ret*x-56;
    ret=ret*x+28;
    ret=ret*x-8;
    ret=ret*x+1;
    return ret;
}

double h(double x)
{
    double t = x - 1;
    double ret = 1;
    for (int i = 0;i<8;i++){
        ret *= t;
    }
    return ret;
}

int main()
{
    //以下为画图部分
    int n = 100;    //描点画图的点数
    double a = 0.99 , b = 1.01;  //区间
    double gap = (b - a) / n;
    //vector<double> x = linspace(0.99, 1.01, n);
    std::vector<double> x(n+1), yf(n+1),yg(n+1),yh(n+1);

    //cout << x.size() << endl;

    plt::figure();          //创建一张图
    for (int i = 0; i < n+1; i++)
    {
        x[i] = a + gap * i;
        yf[i] = f(x[i]);
        yg[i] = g(x[i]);
        yh[i] = h(x[i]);
    }
    plt::plot(x, yf,{{"label", "f(x)"}, {"ls", "--"}});                //
    plt::plot(x, yg,{{"label", "g(x)"},{"color","k"}});
    plt::plot(x, yh,{{"label", "h(x)"},{"color","r"}});

    plt::xlim(a, b);
    plt::title("functions in A"); // set a title
    plt::legend();                // enable the legend

    plt::save("A.png");

    
    cout << "A : Plot output success!" << endl;
    //cout << "pass" << endl;
    return 0;
}